package eu.fishcode.android.utils;

import java.awt.Component;
import java.awt.Font;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;

public class Utils {

	public static class LabelListRenderer extends DefaultListCellRenderer {

		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			final JLabel label = (JLabel) super.getListCellRendererComponent(
					list, value, index, isSelected, cellHasFocus);

			if (value != null) {
				label.setText(((JLabel) value).getText());
				label.setIcon(((JLabel) value).getIcon());
			}
			if (isSelected) {
				label.setForeground(label.getForeground().darker().darker());
			}
			label.setOpaque(false);
			this.setOpaque(false);
			return label;
		}

	}

	public static final String FileSeparator = System
			.getProperty("file.separator");
	public static final String ConfigPath = "Config" + FileSeparator
			+ "config.properties";

	public static final Logger logger;
	public static final String LogPath = "Log" + FileSeparator + "LogFull.html";
	private static final String OsName = System.getProperty("os.name")
			.toLowerCase();

	public static final String RemoteZipPath = "/system/media/bootanimation.zip";

	private static ResourceBundle resourceBundle = null;

	public static final String UserHome = System.getProperty("user.home");
	public static final String WorkZipPath = "Work" + FileSeparator
			+ "bootanimation.zip";

	public static final String WorkPath = "Work" + FileSeparator
			+ "BootAnimation";

	public static final String WorkDescPath = "Work" + FileSeparator
			+ "BootAnimation" + FileSeparator + "desc.txt";

	public static Font ubuntuFont;
	public static Font minecraftFont;

	static {
		// logger
		logger = Logger.getLogger("eu.fishcode.android");
		FileHandler fh = null;
		try {
			fh = new FileHandler(LogPath);
		} catch (final Exception e) {
		}
		logger.addHandler(fh);
		logger.setLevel(Level.ALL);
		fh.setFormatter(new HtmlLogFormatter());

		// font
		final String Ubuntu = "/eu/fishcode/android/resource/font/Ubuntu-M.ttf";
		try {
			final InputStream is = Utils.class.getClass().getResourceAsStream(
					Ubuntu);
			ubuntuFont = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(
					Font.PLAIN, 10);
		} catch (final Exception e) {
			ubuntuFont = Font.getFont(Font.SERIF).deriveFont(Font.PLAIN, 10);
		}
		final String Minecraft = "/eu/fishcode/android/resource/font/minecraft.ttf";
		try {
			final InputStream is = Utils.class.getClass().getResourceAsStream(
					Minecraft);
			minecraftFont = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(
					Font.PLAIN, 10);
		} catch (final Exception e) {
			minecraftFont = Font.getFont(Font.SERIF).deriveFont(Font.PLAIN, 10);
		}
	}

	public static void cleanDirectory(File path) {
		if (path.exists()) {
			final File[] files = path.listFiles();
			for (final File file : files) {
				if (file.isDirectory()) {
					cleanDirectory(file);
				}
				file.delete();
			}
		}
	}

	public static ImageIcon getIcon(String icon) {
		URL imgURL = null;
		ImageIcon imgIcon = null;
		String path = null;
		path = "/eu/fishcode/android/resource/" + icon;
		imgURL = Utils.class.getClass().getResource(path);
		if (imgURL != null) {
			imgIcon = new ImageIcon(imgURL);
		} else {
			logger.log(Level.WARNING, "load failed " + path);
		}

		return imgIcon;
	}

	public static String getLabel(String label) {
		if (resourceBundle == null) {
			if (Configuration.getString("settings.ui.locale")
					.equals("Italiano")) {
				resourceBundle = ResourceBundle.getBundle("LanguagePack",
						Locale.ITALIAN);
			} else if (Configuration.getString("locale").equals("English")) {
				resourceBundle = ResourceBundle.getBundle("LanguagePack",
						Locale.ENGLISH);
			} else {
				resourceBundle = ResourceBundle.getBundle("LanguagePack",
						Locale.getDefault());
			}
		}
		return resourceBundle.getString(label);
	};

	public static boolean isLinux() {
		return (OsName.indexOf("linux") >= 0);
	}

	public static boolean isWindows() {
		return (OsName.indexOf("windows") >= 0);
	}

	public static File[] renameFile(File[] filePart) {
		final DecimalFormat df = new DecimalFormat("0000");
		Arrays.sort(filePart);
		for (int i = 0; i < filePart.length; i++) {
			String fileName = df.format(i + 1) + ".png";
			fileName = filePart[i].getParent() + Utils.FileSeparator + fileName;
			final File newfile = new File(fileName);
			if (filePart[i].renameTo(newfile)) {
				filePart[i] = newfile;
			} else {
				System.out.println("Rename failed");
			}
		}
		return filePart;
	}

}
